package com.diandireader.activity;

import java.io.File;
import java.io.InputStream;

import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.content.SharedPreferences;
import android.content.SharedPreferences.Editor;
import android.os.Bundle;
import android.os.Environment;
import android.view.Window;
import android.view.WindowManager;
import android.view.animation.AlphaAnimation;
import android.view.animation.Animation;
import android.view.animation.Animation.AnimationListener;
import android.widget.RelativeLayout;
import android.widget.Toast;

import com.diandireader.activity.R;
import com.diandireader.domain.DiandiReaderVO;
import com.diandireader.util.ActivityManager;
import com.diandireader.util.FileHelper;
/**
 * 阅读器欢迎界面 
 * @author mayh
 *
 */
public class WelcomeActivity extends Activity {
   
	private SharedPreferences preferences;
	private static final String defaultBook = "明朝那些事.txt";
	private static final String defaultDB = "diandireader.sqlite";
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
//      去掉标题栏
        this.requestWindowFeature(Window.FEATURE_NO_TITLE); 
//      去掉状态栏
        this.getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN,WindowManager.LayoutParams.FLAG_FULLSCREEN);
        setContentView(R.layout.welcome);
        preferences = getSharedPreferences(DiandiReaderVO.SETTING, Context.MODE_PRIVATE);
        ActivityManager.add(this);
    }
	@Override
	protected void onResume() {
		super.onResume();
		//设置渐变
        Animation animation = new AlphaAnimation(0.1f,1.0f);
        animation.setDuration(1000);
        RelativeLayout layout = (RelativeLayout)findViewById(R.id.welcomeLayout);
        layout.startAnimation(animation);
        animation.setAnimationListener(new AnimationListener() {
        	//开始渐变
			@Override
			public void onAnimationStart(Animation animation) {
				
			}
			
			@Override
			public void onAnimationRepeat(Animation animation) {
				
			}
			//渐变完毕
			@Override
			public void onAnimationEnd(Animation animation) {
				String initState = preferences.getString(DiandiReaderVO.INITSTATE, DiandiReaderVO.INITSTATE_UNINIT);
//				在这里进行初始化
				if(initState.equals("0")){
				Toast.makeText(WelcomeActivity.this, "首次使用初始化", Toast.LENGTH_SHORT).show();
				//创建文件目录
				String sdState = Environment.getExternalStorageState();	
				//如果有写权限
				if(sdState.equals(Environment.MEDIA_MOUNTED)){
					String sdForder = FileHelper.getStrogeFolder();
					if(sdForder==null){
						Toast.makeText(WelcomeActivity.this, "创建目录失败,内存容量不足", Toast.LENGTH_SHORT).show();
						return;
					}
//					create folder 
					File diandiReaderBooksFolder = new File(sdForder+"/"+FileHelper.diandiReaderBooks);
					File diandiReaderDbFolder = new File(sdForder+"/"+FileHelper.diandiReaderDb);
					//create book dir
					if(!diandiReaderBooksFolder.exists()){
							diandiReaderBooksFolder.mkdirs();
					}
					//create the db dir
					if(!diandiReaderDbFolder.exists()){
							diandiReaderDbFolder.mkdirs();
					}
					//copy the books
					InputStream inputStream = getResources().openRawResource(R.raw.mingchao);
					File bookFile = new File(diandiReaderBooksFolder.getAbsoluteFile()+"/"+defaultBook);
					FileHelper.copyFile(inputStream, bookFile);
					//copy the db
					InputStream inputStreamDB = getResources().openRawResource(R.raw.diandireader);
					File bookFileDB = new File(diandiReaderDbFolder.getAbsoluteFile()+"/"+defaultDB);
					FileHelper.copyFile(inputStreamDB, bookFileDB);
					//update the init state
					Editor edit = preferences.edit();
					edit.putString(DiandiReaderVO.INITSTATE,DiandiReaderVO.INITSTATE_INITED);
					edit.putInt(DiandiReaderVO.FONTCOLOR, DiandiReaderVO.FONTCOLOR_DEFAULT);
					edit.putInt(DiandiReaderVO.BACKGROUNDIMAGE, DiandiReaderVO.BACKGROUNDIMAGE_DAY);
					edit.commit();
				}else {
					Toast.makeText(WelcomeActivity.this, "没有写权限", Toast.LENGTH_SHORT).show();
				}
				}
				Intent intent = new Intent();
				//load the specify page view
				String viewType  = preferences.getString(DiandiReaderVO.VIEWTYPE, DiandiReaderVO.VIEWTYPE_ICON);
				if("1".equals(viewType))
				intent.setClass(WelcomeActivity.this, MainReaderIconActivity.class);
				else 
				intent.setClass(WelcomeActivity.this, MainReaderListActivity.class);	
				startActivity(intent);
				finish();
			}
		});
	}
}